#encoding=utf8
'''
Created on 2012-10-17

@author: Administrator
'''
from Processor import Processor;
from pub import Func, UserDict, RoomDict;
from pub.Config import Config;
from models import IdiomModel;
class RIGClass(Processor):
    '''
    指令：RIG(room idiom game)
    备注：接龙过程
    格式：c-s:cmd #cmdID#roomID#userID#previousEndKeyword#nextKeyword
     s-c:cmd#cmdID#roomID#userID#userRight#userIDNext#keyword#userGameIn# userGameOut# userGameWait#msg
    '''
    def __init__(self,):
        Processor.__init__(self);
        self.__roomId = 0;
        self.__userId = 0;
        self.__preEndKeyword = "";
        self.__nextKeyword = "";#用户回答的成语
        
        
    def dataValidtor(self, cmdContent):
        if not cmdContent:
            return False;
        paraLen = len(cmdContent);
        if paraLen == 4:
            self.__roomId = Func.strToInt(cmdContent[0], Config.I_SYS_ZERO);
            self.__userId = Func.strToInt(cmdContent[1], Config.I_SYS_ZERO);
            self.__preEndKeyword = Func.toStr(cmdContent[2], "");
            self.__nextKeyword = Func.toStr(cmdContent[3], "");      
            return True;        
        return False;
    def outData(self,):
        room = RoomDict.getRoomFromDictById(self.__roomId);
        if not room:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_ROOM_DETAIL_NULL + ",roomId:" + Func.toStr(self.__roomId, 0));
            return False;
        
        user = RoomDict.findUserInRoom(self.__userId)[1];
        if not user:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_ROOM_USER_NO+ ",userId:" + Func.toStr(self.__userId, 0));
            return False;
        
        
        
        userRight = Config.I_SYS_ONE;
        msg = Config.ROOM_MESSAGE_CODE_IDIOM_SUSS;
        if room.get_used_idiom().has_key(self.__nextKeyword):#已使用过
            userRight = Config.I_SYS_ZERO;
            msg = Config.ROOM_MESSAGE_CODE_IDIOM_USED;
            user.set_user_state(Config.I_USER_GAME_STATE_OVER);
        if not Func.validPinyin(self.__preEndKeyword, self.__nextKeyword):#首尾音不对
            userRight = Config.I_SYS_ZERO;
            msg = Config.ROOM_MESSAGE_CODE_IDIOM_FAIL;
            user.set_user_state(Config.I_USER_GAME_STATE_OVER);
        user.set_current_in_room(False);#设置当前非回答者
        idiomM = IdiomModel();
        if not idiomM.validIdiom(self.__nextKeyword):#非成语
            userRight = Config.I_SYS_ZERO;
            msg = Config.ROOM_MESSAGE_CODE_IDIOM_NO;
            user.set_user_state(Config.I_USER_GAME_STATE_OVER);
        
        user.set_user_state(Config.I_USER_GAME_STATE_OK);
        
        
        room.addIdiom(self.__nextKeyword);
        if userRight==Config.I_SYS_ONE:
            endKeyword=Func.subString(self.__nextKeyword,1, True);#最后一个字
            room.set_current_word(endKeyword);
        nextUserId = self.__userId;
        nextUserNickName="";
        nextUserPhoto="";
        nextUser = RoomDict.getNextUserByPreUserId(room, self.__userId);
        if nextUser:
            nextUser.set_current_in_room(True);
            nextUserId = nextUser.get_user_id();
            nextUserNickName = nextUser.get_user_nick_name();
            nextUserPhoto = nextUser.get_user_photo();
        
        
        
        (okNum, overNum, seeNum) = RoomDict.getUserStateinRoom(room);
        sendStr = self.setPackage([self.__roomId, self.__userId, userRight, nextUserId,nextUserNickName,nextUserPhoto, self.__nextKeyword, okNum, overNum, seeNum, msg]);
        Func.debugInfo("RIG:" + sendStr);
        RoomDict.sendInfoToRoomAllUser(roomObj=room, cmd="", msg=sendStr, isPackage=False);
        return True;        